﻿using DBSync.wpfPage;
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;
using System.Windows.Controls.Primitives;
using System.Windows.Threading;
using Hardcodet.Wpf.TaskbarNotification;
using DBSync.Util;
using DbSync;

namespace DBSync
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private static MainWindow instance;
        public MainWindow()
        {
            InitializeComponent();
            if (SessionData.user != null)
            {
                ContentFrame.Content = HomePage.Instance;
            }
            else
            {
                ContentFrame.Content = Login.Instance;
            }

            //Thread toastMethodThread = new System.Threading.Thread(delegate()
            //{
                //InternetDetector.detectInternetConnection();
                //if (InternetDetector.getConnectionStatus())
                //{
                //    //this.Hide();
                //   // showToastMessage();
                //}
            //});
            //toastMethodThread.SetApartmentState(ApartmentState.STA);
            //toastMethodThread.Start();

        }

        public static MainWindow Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new MainWindow();
                }

                return instance;
            }
        }

        public void showToastMessage()
        {

            //TaskbarIcon notifyIcon = new TaskbarIcon();
            //notifyIcon.ToolTipText = Messages.TTL_APP;
            //notifyIcon.Visibility = Visibility.Visible;
            //FancyBalloon balloon = new FancyBalloon();
            //balloon.BalloonText = "Database Synchronizer";
            //notifyIcon.TrayPopup = balloon;
            //notifyIcon.ShowCustomBalloon(balloon, PopupAnimation.Slide, 4000);
            /*
            InternetDetector.detectInternetConnection();
            if (InternetDetector.getConnectionStatus())
            {
                this.Hide();
                showToastMessage();
            }
            */
            //FancyBalloon balloon = new FancyBalloon();
            //balloon.BalloonText = "Database Synchronizer";

            ////show balloon and close it after 4 seconds
            //Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() =>
            //{
            //    new TaskbarIcon().ShowCustomBalloon(balloon, PopupAnimation.Slide, 4000);
            //    // NotifyIcon.ShowCustomBalloon(balloon, PopupAnimation.Slide, 4000);
            //}));

        }

        private void btn_settings_Click(object sender, RoutedEventArgs e)
        {
            ContentFrame.Content = SettingsPage.Instance;
            btn_home.Visibility = System.Windows.Visibility.Visible;
            
        }

        protected override void OnStateChanged(EventArgs e)
        {
            if (WindowState == WindowState.Minimized)
            {
                this.Hide();

            }
            
            base.OnStateChanged(e);
        }
        
        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            this.Hide();
        }

        private void Window_StateChanged(object sender, EventArgs e)
        {

        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            ConfirmationWindow exit = new ConfirmationWindow(Messages.TTL_EXIT, Messages.MSG_EXIT_CONFIRM);
            exit.ShowDialog();
            if (ConfirmationWindow.isConfirm == MessageBoxResult.Yes)
            {
                this.Close();
                Application.Current.Shutdown();
            }
        }

        private void Button_Click_2(object sender, System.Windows.RoutedEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
        }

        private void btn_home_Click(object sender, RoutedEventArgs e)
        {
            ContentFrame.Content = HomePage.Instance;
            btn_home.Visibility = System.Windows.Visibility.Hidden;
           
        }

        private void Image_ImageFailed(object sender, ExceptionRoutedEventArgs e)
        {

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            btn_home.Visibility = System.Windows.Visibility.Hidden;
        }

        private void btn_add_organization_Click(object sender, RoutedEventArgs e)
        {
            MainWindow.Instance.ContentFrame.Content = OrganizationPage.Instance;
        }

        private void btn_view_organization_Click(object sender, RoutedEventArgs e)
        {
            MainWindow.Instance.ContentFrame.Content = ViewUser.Instance;
        }

        private void btn_logout_Click(object sender, RoutedEventArgs e)
        {
            ConfirmationWindow logout = new ConfirmationWindow(Messages.TTL_LOGOUT, Messages.MSG_LOGOUT_CONFIRM);
            logout.ShowDialog();
            if (ConfirmationWindow.isConfirm == MessageBoxResult.Yes)
            {
                SessionData.user = null;
                SessionData.organization = null;
                MainWindow.Instance.btn_logout.Visibility = System.Windows.Visibility.Hidden;
                MainWindow.Instance.btn_changePassword.Visibility = System.Windows.Visibility.Hidden;
                HomePage.Instance.resetProgress();
                MainWindow.Instance.btn_add_organization.Visibility = System.Windows.Visibility.Hidden;
                MainWindow.Instance.btn_view_organization.Visibility = System.Windows.Visibility.Hidden;
                MainWindow.Instance.ContentFrame.Content = Login.Instance;
            }

        }

        private void btn_changePassword_Click(object sender, RoutedEventArgs e)
        {
            LoginSettingsPage.FromPage = this.ContentFrame.Content;
            MainWindow.Instance.ContentFrame.Content = LoginSettingsPage.Instance;
        }
        
    }
}
